package com.neueedu.android;

import android.app.Person;
import android.content.Context;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.Serializable;

public class LoginActivity extends AppCompatActivity {



    Button btnLogin;
    EditText etUserName;
    EditText etPassword;

    SharedPreferences user_info = null;

    //在显示之前，用于初始化资源（加载布局）
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        System.out.println("===onCreate====");
        Log.v("LoginActivity","==onCreate===");
        Log.d("LoginActivity","==onCreate===");
        Log.i("LoginActivity","==onCreate===");
        Log.w("LoginActivity","==onCreate===");
        Log.e("LoginActivity","==onCreate===");

        user_info  = getSharedPreferences("user_info", Context.MODE_PRIVATE);

        String token  = user_info.getString("token","");
        if(!"".equals(token)){
            toMainActivity();
            return;
        }


        btnLogin = findViewById(R.id.btnLogin);
        etUserName = findViewById(R.id.etUserName);
        etPassword = findViewById(R.id.etPassword);


        //添加点击事件

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                //如果没有输入用户名、密码，给个提示

                String username = etUserName.getText().toString();
                String password = etPassword.getText().toString();
                if("".equals(username)){
                    etUserName.setError("请输入用户名");
                    return;
                }
                if("".equals(password)){
                    etPassword.setError("请输入密码");
                    return;
                }


                //请求网络，验证用户名密码是否正确    .........
                String  token  = "asasdfasfasfasfasfasfdasfasfasfasfasfasfasfasfasfasfafaffafafaffafafafa";


                if("admin".equals(username) && "123456".equals(password)){


                    logUserToken(token);


                    toMainActivity();
                    //
                }else{
                    etUserName.setError("用户名或者密码错误");
                }








                //结束当前 的Activity
//                finish();
            }
        });

    }

    private void toMainActivity() {
        //intent 显示意图
        Intent intent = new Intent(LoginActivity.this , MainActivity.class);
//        //通过意图传递数据
//        intent.putExtra("username",etUserName.getText());
//        intent.putExtra("password",etPassword.getText());
//
//        //设置自定义的类型
//        intent.putExtra("peron",new Person());




        startActivity(intent);
    }

    private void logUserToken(String token) {
        //将token写入文件



        //编辑内容
        SharedPreferences.Editor edit = user_info.edit();
        edit.putString("token",token);


        //写入文件
        edit.commit();

    }


    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("===onStart====");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("===onResume====");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("===onRestart====");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("===onPause====");
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("===onStop====");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("===onDestroy====");

    }



    public static class Person implements Serializable {

    }

}
